import React from 'react';

//react-router;
import { Link } from 'react-router';

//component
import ReButton from '../../widgets/ReButton';

export default class view extends React.Component {
    static propTypes = {
        embedContent: React.PropTypes.string
    };

    static defaultProps = {
        embedContent: ''
    };

    constructor(props, context) {

        super(props, context);


        // init state;
        this.state = {
            embedContent: props.embedContent,
            msg: ''
        };
    }

    componentWillReceiveProps(nextProps) {
        this.setState({
            embedContent: nextProps.embedContent,

        });
    }

    componentWillMount() {
        this._isMounted = true;

    }

    componentDidMount() {

    }

    componentWillUnmount() {
        this._isMounted = false;
    }

    handelCopy(e) {
        e.select();
        if (document.execCommand) {

            document.execCommand('copy');
            this.setState({
                msg: $$.LN.COPY_SUCCESS
            });
            clearTimeout(this.msgTimer);
            this.msgTimer = setTimeout(() => {
                this.setState({
                    msg: ''
                });
            }, 3000);
            //alert($$.LN.COPY_SUCCESS);
        } else {
            this.setState({
                msg: $$.LN.COPY_FAIL
            });

            clearTimeout(this.msgTimer);
            this.msgTimer = setTimeout(() => {
                this.setState({
                    msg: ''
                });
            }, 3000);
            //alert($$.LN.COPY_FAIL);
        }
    }

    handelBtn() {
        this.refs.embed.focus();
    }

    renderComponent() {

        return this.renderEmbed();

    }

    renderEmbed() {
        return (
            <div className="embed-box">
                <textarea 
                    className="textarea"
                    type="text" 
                    value={this.state.embedContent}
                    ref="embed"
                    onFocus = {(e)=>{this.handelCopy(e.target);}}
                >
                </textarea>
                {/*<p className="text">{this.state.msg}</p>*/}
                {/*<ReButton 
                    className="btn"
                    label={$$.LN.COPY}
                    onTouchTap={(e) => { this.handelBtn() }}
                />*/}
            </div>
        );
    }

    render() {

        return (
            <div className="orin-sw-recopyembed-root" >
                {this.renderComponent()}
            </div>
        );
    }
}
